package cn.stylefeng.guns.modular.api;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import cn.stylefeng.guns.modular.system.model.BusinessCategory;
import cn.stylefeng.guns.modular.system.service.IBusinessCategoryService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 服务大类控制器
 *
 * @author glb
 * @Date 2019-10-23 16:49:47
 */

@Validated
@RestController
@RequestMapping("/api/v1//businessCategorys")
@Api(value = "家政大类信息",tags={"家政大类操作接口"})
public class BusinessCategoryApiController extends BaseController {


    @Autowired
    private IBusinessCategoryService businessCategoryService;

    /**
     * 获取服务大类列表
     */
    @RequestMapping(value = "/",method=RequestMethod.GET)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "cateId", value = "大类英文缩写", dataType = "String"),
        @ApiImplicitParam(name = "isValid", value = "是否有效" , dataType ="int",  example="1")
    })
    @ResponseBody
    @ApiOperation(value="获取家政大类列表", notes="")
    public Object list(String cateId,Integer isValid) {
    	EntityWrapper<BusinessCategory> wrapper = new EntityWrapper<>();
    	if(StringUtils.isNotBlank(cateId)) {
    		wrapper.eq("cate_id", cateId);
    	}
    	if(null !=isValid) {
    		wrapper.eq("is_valid", isValid);
    	}
    	wrapper.orderBy("cate_seq",true);
        return businessCategoryService.selectList(wrapper);
    }

    /**
     * 新增服务大类
     */
    @ApiOperation(value="创建家政大类", notes="根据BusinessCategory对象创建家政大类")
    @RequestMapping(value = "/",method=RequestMethod.POST)
    @ApiImplicitParam(name = "businessCategory", value ="家政大类(cateId必须唯一)", required = true ,dataType ="BusinessCategory")
    @ResponseBody
    public Object add(@RequestBody BusinessCategory businessCategory) {
    	Date now =  new Date();
    	businessCategory.setCreateTime(now);
    	businessCategory.setUpdateTime(now);
        businessCategoryService.insert(businessCategory);
        return SUCCESS_TIP;
    }

    /**
     * 删除服务大类
     */
    @ApiOperation(value="删除家政大类", notes="根据url的id来指定删除家政大类对象")
    @ApiImplicitParam(name = "id", value ="家政大类ID", required = true ,dataType ="int",  example="1")
    @RequestMapping(value = "/{id}",method=RequestMethod.DELETE)
    @ResponseBody
    public Object delete(@PathVariable  Integer id) {
        businessCategoryService.deleteById(id);
        return SUCCESS_TIP;
    }

    /**
     * 修改服务大类
     */
    @ApiOperation(value="更新家政大类详细信息", notes="根据url的id来指定更新对象，并根据传过来的BusinessCategory信息来更新家政大类详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "家政大类ID", required = true,dataType ="int",  example="1")
    })
    @RequestMapping(value = "/{id}",method=RequestMethod.PUT)
    @ResponseBody
    public Object update( @PathVariable("id") Integer id, @ModelAttribute BusinessCategory businessCategory) {
    	businessCategory.setId(id);
    	businessCategory.setUpdateTime( new Date());
        businessCategoryService.updateById(businessCategory);
        return SUCCESS_TIP;
    }

    /**
     * 服务大类详情
     */

    @ApiOperation(value="获取家政大类详细信息", notes="根据url的id来获取家政大类详细信息")
    @ApiImplicitParam(name = "id", value = "家政大类ID", required = true,dataType ="int",  example="1")
    @RequestMapping(value = "/{id}",method=RequestMethod.GET)
    @ResponseBody
    public Object detail(@PathVariable("id") Integer id) {
        return businessCategoryService.selectById(id);
    }
}
